﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ls_r
{
    class Program
    {
        private static Int32 countFiles;
        private static Int32 countExts;
        static Program()
        {
            countFiles = 0;
            countExts = 0;
        }
        private static ExtInfo[] extentions= new ExtInfo[200];
        static void Main()
        {
            String[] drives = Directory.GetLogicalDrives();
            for (int i = 0; i < extentions.Length; i++)
            {
                extentions[i] = new ExtInfo();
            }
            
            foreach (String path in drives)
            {
                if (Directory.Exists(path))
                {
                    // This path is a directory
                    ProcessDirectory(path);
                }
                else
                {
                    Console.WriteLine("{0} is not a valid file or directory.", path);
                }
            }
            Console.WriteLine(countFiles);
            SortExtArr(extentions);
            for (int i = 0; i<extentions.Length;i++ )
            {
                extentions[i].GetExtInfo();
            }
            Console.ReadKey();
        }
        private static Int32 IsNew(String ext)
        {
            for(int i=0;i<extentions.Length;i++)
            {
                if (ext == extentions[i].GetExt()) return i;
            }
            return 0;
        }
        private static void SortExtArr(ExtInfo[] extentions)
        {
            for (int i = 0;i<extentions.Length ;i++ )
            {
                for(int j=0;j<extentions.Length-1;j++)
                {
                    if(extentions[j].GetSpace()>extentions[j+1].GetSpace())
                    {
                        ExtInfo temp = extentions[j];
                        extentions[j] = extentions[j + 1];
                        extentions[j + 1] = temp;
                    }
                }
            }
        }
        public static void ProcessFile(string path)
        {
            try
            {
                FileInfo file = new FileInfo(path);
                if (file.Extension == String.Empty) return;
                int index = IsNew(file.Extension);
                if (index == 0)
                {
                    extentions[countExts++].SetExtInfo(file.Extension, file.Length);
                }
                else
                {
                    extentions[index].SetExtInfo(file.Extension, file.Length);
                }

            }
            catch (Exception)
            {

            }
            countFiles++;
        }
        public static void ProcessDirectory(string dirPath)
        {

            try
            {
                string[] files = Directory.GetFiles(dirPath);
                foreach (string fileName in files)
                {
                    ProcessFile(fileName);
                }
            }
            catch (System.UnauthorizedAccessException)
            {
                Console.WriteLine("{0} - access denied", dirPath);
            }
            try
            {
                string[] subDirs = Directory.GetDirectories(dirPath);
                foreach (String subdir in subDirs)
                {
                    ProcessDirectory(subdir);
                }
            }
            catch (System.UnauthorizedAccessException)
            {
                Console.WriteLine("{0} - access denied", dirPath);
            }
        }
    }
}
